package com.example.activitysys.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.file.Paths;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 配置静态资源映射
     * 将 /upload/** 映射到 upload 目录
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 获取项目根目录
        String projectPath = System.getProperty("user.dir");
        String uploadPath = Paths.get(projectPath, "upload").toAbsolutePath().toString();
        
        // 配置静态资源映射
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:" + uploadPath + "/");
        
        // 确保 upload 目录存在
        java.io.File uploadDir = new java.io.File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
    }
}